#-*- coding: utf-8 -*-
#! /usr/bin/python

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import sys
#from selenium.common.exceptions import NoSuchElementException
#from selenium.webdriver.common.keys import Keys
import time

reload(sys)
sys.setdefaultencoding("utf-8")

def loginTb(username,password):
    '''
    jhfnetboy@gmail.com create to login taobao
    '''
    lgurl = 'https://login.taobao.com/member/login.jhtml'
    chrome.get(lgurl)
    #set username password
    try:
        uname = WebDriverWait(chrome, 10).until(lambda chrome : chrome.find_element_by_name("TPL_username"))
        uname.send_keys(username.decode('utf-8'))
    except:
        print 'fail to get element'
    try:
        ckbox = WebDriverWait(chrome, 10).until(lambda chrome : chrome.find_element_by_id("J_SafeLoginCheck"))
        ckbox.click()
    except:
        print 'fail to get element J_SafeLoginCheck'
    try:
        pword = WebDriverWait(chrome, 10).until(lambda chrome : chrome.find_element_by_name("TPL_password"))
        pword.send_keys(password)
    except:
        print 'fail to get element TPL_password'
    try:
        btn = WebDriverWait(chrome, 10).until(lambda chrome : chrome.find_element_by_id("J_SubmitStatic"))
        btn.click()
    except:
        print 'fail to get element J_SubmitStatic'
    

def logoutTb():
    '''
    logout from taobao
    '''
    lurl = "http://login.taobao.com/member/logout.jhtml?spm=a1z02.1.0.6.wXTWrc&amp;f=top&amp;out=true&amp;redirectURL=http%3A%2F%2Fi.taobao.com%2Fmy_taobao.htm%3Fnekot%3Ds6S6%252BtfTtcTQodHy1351908154890"
    chrome.get(lurl)

def pai(item, count):
    '''
    buy url item
    '''
    chrome.get(item)
    chrome.switch_to_frame("J_BundleDetail")  
    try:#submit form
        fm = chrome.find_element_by_id('J_FrmBid')
        fm.submit()
    except:
        print 'fail to get element J_FrmBid'
    try:#submit order #anoymous
        anonybox = WebDriverWait(chrome, 10).until(lambda chrome : chrome.find_element_by_id("anony"))
        anonybox.click()
    except:
        print 'fail to get element anony'
    try:#set count
        chrome.execute_script("document.getElementById('quantityInput').value = "+count)
        cnt = WebDriverWait(chrome, 10).until(lambda chrome : chrome.find_element_by_id("quantityInput"))
        cnt.click()
    except:
        print 'fail to get element quantityInput'
    try:#submit      
        sbt = WebDriverWait(chrome, 10).until(lambda chrome : chrome.find_element_by_id("performSubmit"))
        sbt.submit()
    except:
        print 'fail to get element performSubmit'
        
        

def fastpay(card,ppword):
    '''
    快捷支付，不需要网银
    '''
    time.sleep(1)
    try:#select card
        card = chrome.find_element_by_link_text("储蓄卡".decode('utf-8'))
        card.click()
    except:
        print 'fail to get element card'
    try:#select jianhang
        jrd = chrome.find_element_by_id('J-debit_express-ccb906-3')
        jrd.click()
    except:
        print 'fail to get element jianhang'
    try:#next to pay            
        next = WebDriverWait(chrome, 10).until(lambda chrome : chrome.find_element_by_id("bankCardBtn"))
        next.click()
    except:
        print 'fail to get element bankCardBtn' 
    
    

chrome = webdriver.Chrome()
username = '长胡子的小羊'
password = 'tq3jx9'
item = "http://meal.taobao.com/mealDetail.htm?spm=0.0.0.9.3g3BDQ&meal_id=6718908&seller_id=218776"
count = '10'
card = '6222 8014 6362 1040 844'
ppword = '198276'
loginTb(username,password)
time.sleep(1)
pai(item, count)
fastpay(card, ppword)
time.sleep(1)
#logoutTb()